﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.BL;
using System.Data;
using MandalaySoft.Common;

public partial class Grade_GradeScale : System.Web.UI.Page
{
    MyError _error = new MyError();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[Global.Session_SchoolID] == null)
        {
            Response.Redirect("~/Default.aspx");
        }

        if (!IsPostBack)
        {
            BindData();

            
        }
    }

    private void BindData()
    {
        GradeScaleDB _db = new GradeScaleDB();
        DataTable _dt = new DataTable("tblGradeScale");
        _error = new MyError();
        _dt = _db.GetGradeScaleList(Convert.ToInt32(Session[Global.Session_SchoolID].ToString()), out _error);

        gvGradeScale.DataSource = _dt;
        gvGradeScale.DataBind();
    }

    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        BindData();
        gvGradeScale.PageIndex = e.NewPageIndex;
        gvGradeScale.DataBind();
    }

    protected void AddGradeScale(object sender, EventArgs e)
    {
        string GradeScaleID = ((TextBox)gvGradeScale.FooterRow.FindControl("txtGradeScaleID")).Text;
        string GradeScaleName = ((TextBox)gvGradeScale.FooterRow.FindControl("txtName")).Text;
        string ScaleValue = ((TextBox)gvGradeScale.FooterRow.FindControl("txtScaleValue")).Text;
        bool IsCalculateGPA = ((CheckBox)gvGradeScale.FooterRow.FindControl("chkCalculateGPA")).Checked;
        string SortOrder = ((TextBox)gvGradeScale.FooterRow.FindControl("txtSortOrder")).Text;
        string Comment = ((TextBox)gvGradeScale.FooterRow.FindControl("txtComment")).Text;
        bool IsActive = ((CheckBox)gvGradeScale.FooterRow.FindControl("chkActive")).Checked;

        if (SaveGradeScale(GradeScaleID, GradeScaleName, ScaleValue, IsCalculateGPA, SortOrder, Comment, IsActive) > 0)
        {
            BindData();
        }
    }

    private int SaveGradeScale(string GradeScaleID, string GradeScaleName, string ScaleValue, bool IsCalculateGPA, string SortOrder, string Comment, bool IsActive)
    {
        GradeScale _item = new GradeScale();
        _item.GradeScaleID = Convert.ToInt32(GradeScaleID);
        _item.SchoolID = Convert.ToInt32(Session[Global.Session_SchoolID].ToString());
        _item.Name = GradeScaleName;
        _item.ScaleValue = Convert.ToDecimal(ScaleValue);
        _item.IsCalculateGPA = IsCalculateGPA;
        _item.SortOrder = Convert.ToInt16(SortOrder);
        _item.Comment = Comment;
        _item.IsActive = IsActive;

        GradeScaleDB _db = new GradeScaleDB();
        _item = _db.Save(_item);

        return _item.GradeScaleID;
    }

    protected void UpdateGradeScale(object sender, GridViewUpdateEventArgs e)
    {
        string GradeScaleID = ((Label)gvGradeScale.Rows[e.RowIndex].FindControl("lblGradeScaleID")).Text;
        string GradeScaleName = ((TextBox)gvGradeScale.Rows[e.RowIndex].FindControl("txtName")).Text;
        string ScaleValue = ((TextBox)gvGradeScale.Rows[e.RowIndex].FindControl("txtScaleValue")).Text;
        bool IsCalculateGPA = ((CheckBox)gvGradeScale.Rows[e.RowIndex].FindControl("chkCalculateGPA")).Checked;
        string SortOrder = ((TextBox)gvGradeScale.Rows[e.RowIndex].FindControl("txtSortOrder")).Text;
        string Comment = ((TextBox)gvGradeScale.Rows[e.RowIndex].FindControl("txtComment")).Text;
        bool IsActive = ((CheckBox)gvGradeScale.Rows[e.RowIndex].FindControl("chkActive")).Checked;

        if (SaveGradeScale(GradeScaleID, GradeScaleName, ScaleValue, IsCalculateGPA, SortOrder, Comment, IsActive) > 0)
        {
            gvGradeScale.EditIndex = -1;
            BindData();
        }
    }

    protected void DeleteGradeScale(object sender, EventArgs e)
    {
        LinkButton lnkRemove = (LinkButton)sender;
        GradeScaleDB _db = new GradeScaleDB();
        _error = new MyError();
        _db.Delete(Convert.ToInt32(lnkRemove.CommandArgument), out _error);
        if (_error.ErrorNo == 0)
        {
            BindData();
        }
    }

    protected void EditGradeScale(object sender, GridViewEditEventArgs e)
    {
        gvGradeScale.EditIndex = e.NewEditIndex;
        CheckBox chkCalculateGPA = (CheckBox)gvGradeScale.Rows[e.NewEditIndex].FindControl("chkCalculateGPA");
        Label lblCalculateGPA = (Label)gvGradeScale.Rows[e.NewEditIndex].FindControl("lblCalculateGPA");

        if (chkCalculateGPA != null && lblCalculateGPA != null)
        {
            if (lblCalculateGPA.Text.ToUpper() == "YES")
            {
                chkCalculateGPA.Checked = true;
            }
            else
            {
                chkCalculateGPA.Checked = false;
            }
        }
        BindData();
    }

    protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvGradeScale.EditIndex = -1;
        BindData();
    }

    protected void gvGradeScale_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            if (drv != null)
            {
                Label lblCalculateGPA = (Label)e.Row.FindControl("lblCalculateGPA");
                CheckBox chkCalculateGPA = (CheckBox)e.Row.FindControl("chkCalculateGPA");
                if (lblCalculateGPA != null)
                {
                    if (drv["IsCalculateGPA"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsCalculateGPA"].ToString()) == true)
                        {
                            lblCalculateGPA.Text = "Yes";
                        }
                        else
                        {
                            lblCalculateGPA.Text = "No";
                        }
                    }
                }

                if (chkCalculateGPA != null)
                {
                    if (drv["IsCalculateGPA"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsCalculateGPA"].ToString()) == true)
                        {
                            chkCalculateGPA.Checked = true;
                        }
                        else
                        {
                            chkCalculateGPA.Checked = false;
                        }
                    }
                }


                Label lblActive = (Label)e.Row.FindControl("lblActive");
                CheckBox chkActive = (CheckBox)e.Row.FindControl("chkActive");
                if (lblActive != null)
                {
                    if (drv["IsActive"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsActive"].ToString()) == true)
                        {
                            lblActive.Text = "Yes";
                        }
                        else
                        {
                            lblActive.Text = "No";
                        }
                    }
                }

                if (chkActive != null)
                {
                    if (drv["IsActive"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsActive"].ToString()) == true)
                        {
                            chkActive.Checked = true;
                        }
                        else
                        {
                            chkActive.Checked = false;
                        }
                    }
                }
            }
        }
    }

    protected void gvGradeScale_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //if ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)))
        //{
        //    DataRowView drv = (DataRowView)e.Row.DataItem;

        //    if (drv != null)
        //    {
        //        CheckBox chkCalculateGPA = (CheckBox)e.Row.FindControl("chkCalculateGPA");
        //        CheckBox chkIgnoreSchedule = (CheckBox)e.Row.FindControl("chkIgnoreSchedule");
        //        CheckBox chkActive = (CheckBox)e.Row.FindControl("chkActive");

        //        if (chkCalculateGPA != null)
        //        {
        //            if (drv["IsCalculateGPA"] != null)
        //            {
        //                if (Convert.ToBoolean(drv["IsCalculateGPA"].ToString()) == true)
        //                {
        //                    chkCalculateGPA.Checked = true;
        //                }
        //                else
        //                {
        //                    chkCalculateGPA.Checked = false;
        //                }
        //            }
        //        }


        //        if (chkIgnoreSchedule != null)
        //        {
        //            if (drv["IsIgnoreSchedule"] != null)
        //            {
        //                if (Convert.ToBoolean(drv["IsIgnoreSchedule"].ToString()) == true)
        //                {
        //                    chkIgnoreSchedule.Checked = true;
        //                }
        //                else
        //                {
        //                    chkIgnoreSchedule.Checked = false;
        //                }
        //            }
        //        }


        //        if (chkActive != null)
        //        {
        //            if (drv["IsActive"] != null)
        //            {
        //                if (Convert.ToBoolean(drv["IsActive"].ToString()) == true)
        //                {
        //                    chkActive.Checked = true;
        //                }
        //                else
        //                {
        //                    chkActive.Checked = false;
        //                }
        //            }
        //        }
        //    }
        //}
    }
    protected void gvGradeScale_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (gvGradeScale.SelectedIndex >= 0)
        {
            string GradeScaleID = ((Label)gvGradeScale.SelectedRow.FindControl("lblGradeScaleID")).Text;
            Session[Global.Session_GradeScaleID] = GradeScaleID;
            Response.Redirect("GradeScaleDetail.aspx");
        }
    }
}